package com.github.hwangrq.springboothomework.boot;

import org.apache.catalina.Context;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.WebResourceRoot;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;
import org.apache.catalina.webresources.DirResourceSet;
import org.apache.catalina.webresources.StandardRoot;

import javax.servlet.ServletException;
import java.io.File;

/**
 * @author: hwangrq
 * @date: 2021/3/9 17:18
 * @description:
 */
public class AppTomcat {

    private static final int PORT = 8081;

    private static final String WEB_APP_PATH = "src/main";

    private static final String WEB_INF_CLASSES_PATH = "target/classes";

    private static final String RESOURCES_PATH = "/WEB-INF/classes";

    public static void main(String[] args) throws ServletException, LifecycleException {
        String s = System.getProperty("user.dir") + File.separator ;
        System.out.println(s);
        start();
    }

    public static void start() throws ServletException, LifecycleException {
        System.out.println("开始启动tomcat");
        //创建tomcat服务
        Tomcat tomcat = new Tomcat();
        //指定端口
        tomcat.setPort(PORT);
        //添加webapp
        //webapp禁止重载入
        StandardContext myCtx = (StandardContext) tomcat.addWebapp("", new File(WEB_APP_PATH).getAbsolutePath());

//        //加载静态资源文件
//        File additionWebInfClasses = new File(WEB_INF_CLASSES_PATH);
//        //创建web应用资源
//        WebResourceRoot resources = new StandardRoot(webapp);
//        //tomcat将提供的web应用资源添加到此Web应用程序。
//        resources.addPreResources(new DirResourceSet(resources,RESOURCES_PATH, additionWebInfClasses.getAbsolutePath(), "/"));
        //启动tomcat
        System.out.println("over");
        tomcat.start();
        System.out.println("启动tomcat完毕");
        //开启异步接收http请求接入
        tomcat.getServer().await();
    }

}
